<!-- 我的订单 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.auditStatus" placeholder="审核状态" clearable style="width: 240px">
          <el-option label="未审核" value='0' />
          <el-option label="通过" value='1' />
          <el-option label="未通过" value='2' />
        </el-select>
      </el-form-item>
      <el-form-item label="还款状态" prop="status">
        <el-select v-model="queryParams.paymentStatus" placeholder="还款状态" clearable style="width: 240px">
          <el-option label="未还款" value='0' />
          <el-option label="已还款" value='1' />
          <el-option label="已逾期" value='2' />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="move-text">
      <p>{{ moveTexts }}</p>
    </div>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="用户名" align="center" prop="realName" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="提交时间" align="center" prop="commitTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请金额" align="center" prop="applymoney" />
      <el-table-column label="贷款期限" align="center" prop="loanTerm">
        <template slot-scope="scope">
          <span v-if="scope.row.loanTerm == '0'">1个月</span>
          <span v-else-if="scope.row.loanTerm == '1'">3个月</span>
          <span v-else-if="scope.row.loanTerm == '2'">6个月</span>
          <span v-else-if="scope.row.loanTerm == '3'">9个月</span>
          <span v-else-if="scope.row.loanTerm == '4'">12个月</span>
          <span v-else-if="scope.row.loanTerm == '5'">18个月</span>
          <span v-else-if="scope.row.loanTerm == '6'">24个月</span>
          <span v-else="scope.row.loanTerm=='7'">36个月</span>
        </template>
      </el-table-column>
      <el-table-column label="还款方式" align="center" prop="paymentMethod">
        <template slot-scope="scope">
          <span v-if="scope.row.paymentMethod == '0'">等额本息</span>
          <span v-else-if="scope.row.paymentMethod == '1'">等额本金</span>
          <span v-else-if="scope.row.paymentMethod == '2'">先息后本</span>
          <span v-else-if="scope.row.paymentMethod == '3'">一次性还息付本</span>
        </template>
      </el-table-column>

      <el-table-column label="还款状态" align="center" prop="paymentStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.paymentStatus == '0'">未还款</span>
          <span v-else="scope.row.paymentStatus=='1'">已还款</span>
          <span v-else="scope.row.paymentStatus=='2'">已逾期</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0" style="color: blue;">未审核</span>
          <span v-else-if="scope.row.auditStatus == 1" style="color: green;">通过</span>
          <span v-else="scope.row.auditStatus==2" style="color: red;">未通过</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.orderStatus == '0'">未发放</span>
          <span v-else="scope.row.orderStatus=='1'">已发放</span>
          <span v-else="scope.row.orderStatus=='2'">失效</span>
        </template>
      </el-table-column>
      <el-table-column label="待还期数" align="center" prop="creRefund.refundCount" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.auditStatus == 1 && scope.row.paymentStatus != 1" type="text"  @click="repayment(scope.row)">单次还款</el-button>
          <el-button v-if="scope.row.auditStatus == 1 && scope.row.paymentStatus != 1" type="text"  @click="disposableRepayment(scope.row)">一次性还款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 详情 -->
    <el-dialog :visible.sync="open" :title="titleL" width="1000px" append-to-body>
      <el-descriptions :title="order.productName" :visible.sync="open" append-to-body direction="vertical" :column="3"
        border>
        <template slot="extra">
          <el-button v-if="titleL == '单次还款'" type="primary" size="small" @click="affirmPay">确认还款</el-button>
          <el-button v-if="titleL == '一次性还款'" type="primary" size="small" @click="disposableAffirmPay">确认一次性还款</el-button>
        </template>
        <el-descriptions-item label="月利率(%)">{{ productInfo.monthRate }}</el-descriptions-item>
        <el-descriptions-item label="咨询费率(%)" :span="2">{{ productInfo.consultingRate }}</el-descriptions-item>
        <el-descriptions-item label="保证金费率(%)">{{ productInfo.marginRate }}</el-descriptions-item>
        <el-descriptions-item label="还款方式">
          <label v-if="order.paymentMethod == 0" >等额本息</label>
          <label v-else-if="order.paymentMethod == 1">等额本金</label>
        </el-descriptions-item>
        <el-descriptions-item label="总还款金额">{{ refundInfo.shouldTotal }}</el-descriptions-item>
        <el-descriptions-item label="已还金额">{{ (refundInfo.actualTotal) }}</el-descriptions-item>
        <el-descriptions-item label="支付方式">{{ radio }}</el-descriptions-item>
        <el-descriptions-item v-if="titleL=='单次还款'" label="剩余还款期数">{{ refundInfo.refundCount }}</el-descriptions-item>
        <el-descriptions-item label="本次还款金额金额"><label style="margin-left: 60%;color:red;font-size: 20px;">{{ refundInfo.monthlyPayment }}元</label></el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <!-- 支付选择 -->
    <el-dialog title="请选择支付方式" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-radio-group v-model="radio">
          <el-radio-button label="微信"></el-radio-button>
          <el-radio-button label="支付宝"></el-radio-button>
          <el-radio-button label="刷卡"></el-radio-button>
        </el-radio-group>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" :page-sizes="[5, 10, 15, 20]" />
  </div>
</template>

<script>
import { userOrderList, getRefundById,getDisRefundById } from "@/api/credit/creOrder";
import { getProducts } from "@/api/credit/creproduct";
import { rAffirmPay,disAffirmPay } from '@/api/credit/creRefund'

export default {
  dicts: ['cre_loan_term', 'sys_normal_disable', 'cre_payment_method', 'cre_refund_status'],
  name: "Order",
  data() {
    return {
      moveTexts: '一次性还款利息有减免哦......',
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      orderList: [],
      // 详情
      order: '',
      // 产品数据
      productInfo: '',
      // 是否显示弹出层
      open: false,
      // 还款信息
      refundInfo: '',
      // 是否展示选择支付方式弹出层
      dialogVisible: false,
      radio:'微信',
      titleL:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        productName: null,
        auditStatus: null,
        paymentStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.moveText()
  },
  methods: {
    //折叠面板方法
    handleChange(val) {

    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      userOrderList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.orderList = response.rows;
        console.log(this.orderList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 5,
        productName: null,
        auditStatus: null,
        paymentStatus: null,
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('credit/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /* 单次还款 */
    repayment(row) {
      this.titleL = '单次还款';
      this.dialogVisible = true;
      // 测算还款金额
      getRefundById(row.orderId).then(res => {
        this.refundInfo = res.data
      }).catch();
      this.open = true;
      this.order = row;
      this.productInfo = '';
      getProducts(row.productId).then(res => {
        this.productInfo = res.data
      }).catch();
    },
    // 一次性还款
    disposableRepayment(row){
      this.titleL = '一次性还款';
      this.dialogVisible = true;
      // 测算还款金额
      getDisRefundById(row.orderId).then(res => {
        this.refundInfo = res.data
      }).catch();
      this.open = true;
      this.order = row;
      this.productInfo = '';
      getProducts(row.productId).then(res => {
        this.productInfo = res.data
      }).catch();
    },
    /* 单次确认还款 */
    affirmPay() {
      rAffirmPay(this.order.userId, this.order.orderId).then(res => {
        this.getList()
        this.open = false
        this.$message({
          message: '恭喜您本次还款成功，您还有:'+(this.refundInfo.refundCount-1)+'期待还',
          type: 'success'
        });
      }).catch()
    },
    /* 一次性确认还款 */
    disposableAffirmPay() {
      disAffirmPay(this.order.orderId).then(res => {
        this.getList()
        this.open = false
        this.$message({
          message: '恭喜您完成此订单的还款任务，期待您的下次光临',
          type: 'success'
        });
      }).catch()
    },
    // 文字滚动
    moveText() {
      // 箭头函数解决 this 指向问题
      setInterval(() => {
        var start = this.moveTexts.slice(0, 1)
        var end = this.moveTexts.slice(1)
        this.moveTexts = end + start
      }, 400)
    },
  }
};
</script>

<style>
  .move-text{
    color: rgba(244, 166, 21, 0.779);
    margin-left: 1000px;
  }
</style>
